import { darkStateAtom } from '@/store'
import { useAtom } from 'jotai'
import { useCallback } from 'react'
import { useConfig } from './Config'

export const useDarkTheme = () => {
  const [isDark, setDark] = useAtom(darkStateAtom)

  const toggleTheme = () => {
    setDark(!isDark)
  }

  const setTheme = (dark: boolean) => {
    setDark(dark)
  }

  return {
    isDark,
    toggleTheme,
    setTheme
  }
}

export const useThemeConfig = () => {
  const { updateConfigField } = useConfig()

  // 更新主题配置到配置文件
  const updateThemeConfig = useCallback(
    async (defaultDarkMode: boolean) => {
      try {
        await updateConfigField('theme.defaultDarkMode', defaultDarkMode)
      } catch (error) {
        console.error('Failed to update theme config:', error)
        throw error
      }
    },
    [updateConfigField]
  )

  return {
    updateThemeConfig
  }
}
